﻿using GBT.Client.FileOperation;
using GBT.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GBT.Client.Forms
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        public string ApiUrl { get; } = "https://network.egbt.net:65506";

        public UserLoginInfo userLoginInfo { get; set; } = new UserLoginInfo();

        public string rootDirectory { get; set; }

        FileManagement fileManagement;

        enum CloseType
        {
            Close,
            Skip
        }

        CloseType closeType;

        private void Login_Load(object sender, EventArgs e)
        {
            string userName = ConfigHelper.GetValue("UserName");
            string password = ConfigHelper.GetValue("Password");
            bool isRememberMe = bool.Parse(ConfigHelper.GetValue("IsRememberMe"));

            txtUserName.Text = userName;
            txtPassword.Text = password;
            cbRememberMe.Checked = isRememberMe;
            if (isRememberMe)
            {
                //记住密码 则自动登陆
                AutoLogin();
            }
        }

        private void AutoLogin()
        {
            try
            {
                userLoginInfo.UserName = txtUserName.Text.Trim();
                userLoginInfo.Password = txtPassword.Text.Trim();
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                if (string.IsNullOrEmpty(userLoginInfo.UserName) || string.IsNullOrEmpty(userLoginInfo.Password))
                {
                    MessageBox.Show("Please enter a user name and password!", "TIPS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (cbRememberMe.Checked)
                {
                    //保存用户名和密码
                    ConfigHelper.SetValue("UserName", userLoginInfo.UserName);
                    ConfigHelper.SetValue("Password", userLoginInfo.Password);
                    ConfigHelper.SetValue("IsRememberMe", "true");
                    
                    keyValuePairs.Add("UserName", userLoginInfo.UserName);
                    keyValuePairs.Add("Password", userLoginInfo.Password);
                    keyValuePairs.Add("IsRememberMe", "true");
                    ConfigHelper.SaveClientConfig(keyValuePairs);
                }
                else
                {
                    //删除保存的用户名和密码
                    ConfigHelper.SetValue("Password", "");
                    ConfigHelper.SetValue("IsRememberMe", "false");

                    keyValuePairs.Add("Password", "");
                    keyValuePairs.Add("IsRememberMe", "false");
                    ConfigHelper.SaveClientConfig(keyValuePairs);
                }

                //执行登陆验证请求
                string body = JsonConvert.SerializeObject(userLoginInfo);
                string result = HttpHelper.PostData(ApiUrl + "/DomainUserVerify", body, "application/json");
                Program.UserInfo = JsonConvert.DeserializeObject<UserInfo>(result);
                if (Program.UserInfo.IsSuccess)
                {
                    Program.isLogin = true;
                    rootDirectory = Program.UserInfo.Properties.displayname[0].Replace("[", "").Replace("]", "");
                    fileManagement = new FileManagement();
                    fileManagement.ftpOperation = new FtpOperation(userLoginInfo.UserName, userLoginInfo.Password);
                    fileManagement.root = rootDirectory;
                    closeType = CloseType.Skip;
                    this.Close();
                }
                else
                {
                    Program.isLogin = false;
                    MessageBox.Show(Program.UserInfo.Path, "TIPS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                Program.isLogin = false;
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "TIPS", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            AutoLogin();
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (closeType == CloseType.Skip)
            {
                fileManagement.Show();
            }
            closeType = CloseType.Close;
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AutoLogin();
            }
        }
    }
}
